package datamining.algorithms.prefixspan;

import java.util.ArrayList;
import java.util.Collection;

import datamining.miner.chain.Extension;
import datamining.miner.chain.GenerationStep;
import datamining.miner.chain.MiningStep;
import datamining.miner.chain.SearchLatticeNode;
import datamining.miner.enviroment.LocalEnvironment;
import datamining.miner.general.Embedding;
import de.parsemis.miner.general.HPEmbedding;

/** 
 * <p>
 * 创建于 2012-12-10 下午10:00:42
 * 
 * @author xujinghu (jinghuxu@nudt.edu.cn)
 * <br>
 * Copyright 2012 xujinghu
 * <br>
 * Lisence: Apache
 */
public class PrefixSpanGeneration<ItemType> extends GenerationStep<ItemType> {
	
	private final Collection<Embedding<ItemType>> embeddings;

	private final LocalEnvironment<ItemType> env;

	private final PThreadEnvironment<ItemType> tenv;

	public PrefixSpanGeneration(MiningStep<ItemType> next, final PThreadEnvironment<ItemType> tenv) {
		super(next);
		// TODO Auto-generated constructor stub
		this.env = LocalEnvironment.env();
		this.embeddings = new ArrayList<Embedding<ItemType>>();
		// ArrayList for deterministic order
		this.tenv = tenv;
	}

	public void call(final SearchLatticeNode<ItemType> node,
			final Collection<Extension<ItemType>> extensions) {
		//TODO:待完善扩展方法
		assert node instanceof PrefixCode;
		reset();
		final PrefixCode<ItemType> code = (PrefixCode<ItemType>) node;
		
		
		super.call(node, extensions);
	}
	
	public void call(final SearchLatticeNode<ItemType> node,
			final PrefixSpanEmbedding<ItemType> embedding) {
		embeddings.add(embedding);
		super.call(node, embedding);
	}
	
	@Override
	public final void reset() {
		// delete old Embeddings;
		if (!env.storeEmbeddings) {
			for (final Embedding<ItemType> emb : embeddings) {
				((PrefixSpanEmbedding<ItemType>) emb).release(tenv);
			}
		}
		embeddings.clear();
		super.reset();
	}
}
